import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import nodejs from "@dfeidao/atom-web/msg/nodejs";
import render from "@dfeidao/atom-web/render/render";
import s_get from '@dfeidao/atom-web/storage/get';
// import whoami from '@dfeidao/atom-web/msg/whoami';
import get_data from '../../atom/get-data';


// import { send_msg as url, get_user_info } from './config';
import { refsystemid, systemid } from '../../atom/config';


import get from '@dfeidao/atom-web/local/get';

import remove from '@dfeidao/atom-web/local/remove';

interface Data {
	modelid: string;
	tablename: string;
	fieldtitle: string;
	search_title: string;
	field: string;
	search_field: string;
	systemid: string;
}
interface Vv {
	w: string;
	v: string;
	arr: object[];

}

import p000 from './p000';
import p001 from './p001';
import p003 from './p003';
import p005 from './p005';

function chunk(arr: object[], size: number) {
	const arr2 = new Array();
	for (let i = 0; i < arr.length; i = i + size) {
		const arr1 = arr;
		arr2.push(arr1.slice(i, i + size));
	}
	return arr2;
}

export default async function a015(fd: IFeidaoAiBrowserComponent) {
	remove(fd, 'ss');
	remove(fd, 'aa');

	const role_no = get(fd, 'role_no');
	const param = get(fd, 'criteria') as {};
	const index = get(fd, 'page_no');
	// console.log(index)
	// const user = await whoami(get_user_info);
	// console.log(user)
	const prid = await s_get('productid');
	const query = {
		filter: {
			productid: prid,
			role_no
		}
	};
	const res = await nodejs("consumer/zj-000007/consumer-find5", query) as Data[];
	// console.log(res)
	const arr = new Array();
	arr.push({ w: res[0].field, n: res[0].fieldtitle });
	arr.push({ w: res[0].search_field, n: res[0].search_title });

	// const aa = await get_data(
	// 	url, res[0].systemid, '',
	// 	res[0].modelid, res[0].tablename,
	// 	{
	// 		paging: { index: 1, size: 0 },
	// 		param
	// 	}, false, false
	// ) as Vv[];
	// console.log(aa)
	const aa = await get_data(systemid, refsystemid, res[0].modelid, res[0].tablename, { paging: { index: 1, size: 0 }, param }) as Vv[];

	if (aa.length === 0) {
		render(fd, '', p001, 'p001', 'inner');
		fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="p005"]').innerHTML = '';
		fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="p003"]').classList.remove('display-n');
		render(fd, aa, p003, 'p003', 'inner');
	} else {
		const data = aa.map((a) => {
			const obj = {} as Vv;
			arr.map((v) => {
				obj[v.w] = a[v.w];
			});
			obj.v = JSON.stringify(obj);
			obj.arr = arr;
			return obj;
		});

		const dataArray = chunk(data, 10);
		const _obj = {} as {
			count: number;
			page_no: number;
			page_size: number;
		};
		// console.log(dataArray)
		_obj.count = data.length;
		_obj.page_no = index as number;
		_obj.page_size = 10;

		const arr1 = arr.map((v) => {
			return v.w;
		});

		const obj1 = { arr1: new Array(), dataArray: new Array() };
		obj1.arr1 = arr1;
		obj1.dataArray = dataArray[index as number - 1];
		render(fd, arr, p000, 'p000', 'inner');
		render(fd, obj1, p001, 'p001', 'inner');
		fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="p003"]').classList.add('display-n');
		render(fd, _obj, p005, 'p005', 'inner');
	}
}
